package com.iot.ic.service;



import com.iot.ic.common.CustomException;
import com.iot.ic.common.Options;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.dao.ManualInterventionMapper;
import com.iot.ic.model.IllegalResultShow;
import com.iot.ic.model.ManualIntervention;
import com.iot.ic.model.ScenesResult;
import com.iot.ic.model.TeamInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by xiuge on 2017/2/20.
 */
@Service
public class ManualInterventionServiceImpl implements ManualInterventionService {

	@Autowired
	private ManualInterventionMapper manualInterventionMapper;
	
	@Autowired
	private ScenesResultService scenesResultService;
	
	@Autowired
	private IllegalResultShowService illegalResultShowService;
	
	@Autowired
	private TeamService teamService;
	
	@Autowired
	SimpMessagingTemplate simpMessagingTemplate;

	@Override
	public int startManualIntervention(ManualIntervention manualIntervention) {
		//获得场景成绩
		ScenesResult dataScenesResult = scenesResultService.getScenesInfoByScenesResultId(manualIntervention.getScenesResultId());
		TeamInfo team = teamService.getTeam(manualIntervention.getTeamId());
		manualIntervention.setStartTime(new Date());
		manualIntervention.setCreateTime(new Date());
		manualIntervention.setDelFlag(0);
		if (manualIntervention.getInterveneScene() == 0) {
			if (manualIntervention.getInterveneType() == 0) {
				manualIntervention.setDeductScore(40);
			} else {
				manualIntervention.setDeductScore(0);
			}
		} else {
			if (manualIntervention.getInterveneType() == 0) {
				manualIntervention.setDeductScore(40);
			} else {
				manualIntervention.setDeductScore(50);
			}
		}
		int result = manualInterventionMapper.insert(manualIntervention);

			
		
		//增加违规结果展示信息
		IllegalResultShow IllegalResultShow = new IllegalResultShow();
		IllegalResultShow.setCreateTime(new Date());
		IllegalResultShow.setDeductScore(manualIntervention.getDeductScore());
		IllegalResultShow.setDelFlag(0);
		IllegalResultShow.setIllegalCount(1);
		if (manualIntervention.getInterveneType() == Options.ManualIntervention.getCode()){
			IllegalResultShow.setIllegalName(Options.ManualIntervention.getDescription());
		} else  {
			IllegalResultShow.setIllegalName(Options.ManualDriving.getDescription());
		}
		IllegalResultShow.setScenesResultId(manualIntervention.getScenesResultId());
		IllegalResultShow.setScenesNo(dataScenesResult.getScenesNo());
		IllegalResultShow.setScenesType(dataScenesResult.getScenesType());
		IllegalResultShow.setTeamId(manualIntervention.getTeamId());
		int showResult = illegalResultShowService.addIllegalResultShow(IllegalResultShow);
		
		//websocket 推送人工干预信息
		if (showResult == 1) {
			Response response=new Response();
			IllegalResultShow.setTeamName(team.getTeamName());
			response.setResult(IllegalResultShow);
			simpMessagingTemplate.convertAndSend("/topic/testsocketpush", response);
			simpMessagingTemplate.convertAndSendToUser(team.getTeamId().toString(), "/testManualInterventionsocketpush", response);
		}
		
		
		if (result > 0) {
			result = manualIntervention.getManualInterveneId();
			//修改场景比赛结果人工干预标志位为1
			ScenesResult scenesResult = new ScenesResult();
			scenesResult.setScenesResultId(manualIntervention.getScenesResultId());
			scenesResult.setManualInterventionFlag(1);
			try {
				scenesResultService.updateScenesResult(scenesResult);
			} catch (Exception e) {
				throw new CustomException(ReturnCode.Err_PostScenesResultFailor);
			}
		}

		return result;
	}

	@Override
	public int endManualIntervention(ManualIntervention manualIntervention) {
		ManualIntervention oldManualIntervention = manualInterventionMapper.selectByPrimaryKey(manualIntervention.getManualInterveneId());
//		
		Date endTime = manualIntervention.getEndTime();
		Date startTime = oldManualIntervention.getStartTime();
		int spendTime = (int) ((endTime.getTime() - startTime.getTime())/1000);
//		if (time < 1200000) {
//			ScenesResult scenesResult = scenesResultService.getScenesInfoByScenesResultId(oldManualIntervention.getScenesResultId());
//			int scenesScore = scenesResult.getScenesScore();
//			int minute = (int) (time/(1000*60)) + 1;
//			int a = scenesScore/20;
//			int deductScore = minute*a;
//			manualIntervention.setDeductScore(deductScore);
//		} else {
//			manualIntervention.setDeductScore(0);
//		}
		manualIntervention.setSpendTime(spendTime);
		int result = manualInterventionMapper.updateByPrimaryKeySelective(manualIntervention);
		return result;
	}

	@Override
	public List<ManualIntervention> listManualInterventionByScenes(int scenesResultId) {
		 List<ManualIntervention> result = manualInterventionMapper.listManualInterventionByScenes(scenesResultId);
		return result;
	}

	@Override
	public int deleteManualIntervention(int manualInterventionId) {
		int resultId = manualInterventionMapper.deleteManualIntervention(manualInterventionId);
		return resultId;
	}

	@Override
	@Transactional
	public int deleteManualInterventionAfter(int manualInterventionId) {
		int result = manualInterventionMapper.deleteManualIntervention(manualInterventionId);
		if (result==1){
			ManualIntervention manualIntervention=manualInterventionMapper.selectByPrimaryKey(manualInterventionId);
			int result1=scenesResultService.updateScenceScore(manualIntervention.getScenesResultId());
			return result1;
		}else {
			throw new CustomException("删除人工干预记录失败");
		}
	}

	@Override
	public List<Map<String, Object>> listManualInterventionByCount(int count, int teamId, String startTime,
			String endTime) {
		List<Map<String, Object>> dataMap = manualInterventionMapper.listManualInterventionByCount(count, teamId, startTime, endTime);
		return dataMap;
	}

	

}
